#include <stdio.h>
#include <string.h>
#include "report.h"

#define N     6 //M0设备的个数
#define M     5 //modbus设备的个数
#define P     6 //mapp设备的个数

struct memory share_memory;
static struct shm_param para_m0;
static struct shm_param para_modbus;
static struct shm_param para_mapp;
struct msgbuf msg_send,msg_rcv;

int main(int argc, char const *argv[])
{
    int ret_memory = -1;//用于共享内存创建成功与否的判断
    //开辟M0设备的共享内存
    ret_memory = shm_init(&para_m0,"shm_m0",sizeof(struct std_node)*N);
    if(ret_memory < 0)
    {
        printf("shm_m0 init error.\n");
        return -1;
    }
    share_memory.memory_m0 = shm_getaddr(&para_m0);
    if (share_memory.memory_m0 == NULL)
    {
        printf("shm_m0 error\n");
        return -1;
    }
    ret_memory = -1;
    
    //开辟modbus的共享内存
    ret_memory = shm_init(&para_modbus,"shm_modbus",sizeof(struct std_node)*M);
    if(ret_memory < 0)
    {
        printf("shm_modbus init error.\n");
        return -1;
    }
    share_memory.memory_modbus = shm_getaddr(&para_modbus);
    if (share_memory.memory_modbus == NULL)
    {
        printf("shm_modbus error\n");
        return -1;
    }
    ret_memory = -1;

    //开辟mapp的共享内存
    ret_memory = shm_init(&para_mapp,"shm_mapp",sizeof(struct std_node)*P);
    if(ret_memory < 0)
    {
        printf("shm_mapp init error.\n");
        return -1;
    }
    share_memory.memory_mapp = shm_getaddr(&para_mapp);
    if(share_memory.memory_mapp == NULL)
    {
        printf("shm_mapp error\n");
        return -1;
    }

    int i;
    for ( i = 0; i < N; i++)
    {
        if (i < 2)
        {
            share_memory.memory_m0[i].type = 3;
            share_memory.memory_m0[i].old_val.f_val = 2.25+i;
            share_memory.memory_m0[i].new_val.f_val = 1.25+i+1;
        }
        else
        {
            share_memory.memory_m0[i].type = 1;
            share_memory.memory_m0[i].old_val.i_val = i*2;
            share_memory.memory_m0[i].new_val.i_val = i*2+1;
        }
        share_memory.memory_m0[i].key = i+1;
        share_memory.memory_m0[i].dev_type = 1;
        share_memory.memory_m0[i].ret = 0;
    }
    
    for ( i = 0; i < M; i++)
    {
        if (i == 0 || i == 1 || i == 3)
        {
            share_memory.memory_modbus[i].type = 3;
            share_memory.memory_modbus[i].old_val.f_val = 2.22+i;
            share_memory.memory_modbus[i].new_val.f_val = 2.45+i+1;
        }
        else
        {
            share_memory.memory_modbus[i].type = 1;
            share_memory.memory_modbus[i].old_val.i_val = i*2;
            share_memory.memory_modbus[i].new_val.i_val = i*2+1;
        }
        
        share_memory.memory_modbus[i].key = 101+1;
        share_memory.memory_modbus[i].dev_type = i+3;
        share_memory.memory_modbus[i].ret = 0;
    }
        
    for ( i = 0; i < P; i++)
    {
        if (i < 3)
        {
            share_memory.memory_mapp[i].type = 3;
            share_memory.memory_mapp[i].old_val.f_val = 3.14*i;
            share_memory.memory_mapp[i].new_val.f_val = 3.22*i+1;
        }
        else
        {
            share_memory.memory_mapp[i].type = 1;
            share_memory.memory_mapp[i].old_val.i_val = 3*i;
            share_memory.memory_mapp[i].new_val.i_val = 3*i+1;
        }
        share_memory.memory_mapp[i].key = 201+i;
        share_memory.memory_mapp[i].dev_type = i+6;
        share_memory.memory_mapp[i].ret = 0;
        
    }
    msg_queue_recv("send_m0",&msg_rcv,sizeof(msg_rcv),0,0);
    printf("%s\n",msg_rcv.mdata);
    msg_send.mtype = 1;
    strcpy(msg_send.mdata,"hello world!");
    msg_queue_send("recv_m0",&msg_send,sizeof(msg_send),0);

    return 0;
}
